﻿// ------------------------------------------------------------------------------
//  © 2008 OpenUpon™.  All rights reserved.
// ------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using Castle.ActiveRecord;
using Sieris.OpenUpon.Framework;

namespace CastUpon.Entities {
	[ActiveRecord]
	public class PodcastChannel : ActiveRecordBase<PodcastChannel> {
		private string author;
		private string authorEmail;
		private string category;
		private string copyright;
		private string description;
		private int id;
		private int imageHeight;
		private string imagePath;
		private int imageWidth;
		private IList<iTunesCategory> itunesCategories = new List<iTunesCategory>();
		private string itunesImagePath;
		private string keywords;
		private string language;
		private IList<Podcast> podcasts = new List<Podcast>();
		private DateTime publishDate;
		private string subTitle;
		private string title;
		private int ttl;
		private string urlHash;

		[PrimaryKey]
		public int Id {
			get { return this.id; }
			set { this.id = value; }
		}

		[Property]
		public string Description {
			get { return this.description; }
			set { this.description = value; }
		}

		[Property]
		public DateTime PublishDate {
			get { return this.publishDate; }
			set { this.publishDate = value; }
		}

		[Property]
		public string Language {
			get { return this.language; }
			set { this.language = value; }
		}

		[Property]
		public string Copyright {
			get { return this.copyright; }
			set { this.copyright = value; }
		}

		[Property]
		public string Author {
			get { return this.author; }
			set { this.author = value; }
		}

		[Property]
		public string AuthorEmail {
			get { return this.authorEmail; }
			set { this.authorEmail = value; }
		}

		[Property]
		public string Category {
			get { return this.category; }
			set { this.category = value; }
		}

		[Property]
		public int Ttl {
			get { return this.ttl; }
			set { this.ttl = value; }
		}

		[Property]
		public string Keywords {
			get { return this.keywords; }
			set { this.keywords = value; }
		}

		[Property]
		public string Title {
			get { return this.title; }
			set { this.title = value; }
		}

		[Property]
		public string SubTitle {
			get { return this.subTitle; }
			set { this.subTitle = value; }
		}

		[Property]
		public string ImagePath {
			get { return this.imagePath; }
			set { this.imagePath = value; }
		}

		[Property]
		public int ImageWidth {
			get { return this.imageWidth; }
			set { this.imageWidth = value; }
		}

		[Property]
		public int ImageHeight {
			get { return this.imageHeight; }
			set { this.imageHeight = value; }
		}

		[Property]
		public string iTunesImagePath {
			get { return this.itunesImagePath; }
			set { this.itunesImagePath = value; }
		}

		[HasMany(Inverse = true, Cascade = ManyRelationCascadeEnum.AllDeleteOrphan)]
		public IList<Podcast> Podcasts {
			get { return this.podcasts; }
			set { this.podcasts = value; }
		}

		[HasMany(Inverse = true, Cascade = ManyRelationCascadeEnum.AllDeleteOrphan)]
		public IList<iTunesCategory> iTunesCategories {
			get { return this.itunesCategories; }
			set { this.itunesCategories = value; }
		}

		public string UrlHash {
			get {
				if(this.urlHash == null) {
					MD5 md5 = new MD5CryptoServiceProvider();
					byte[] originalBytes = Encoding.Default.GetBytes(Id.ToString());
					byte[] encodedBytes = md5.ComputeHash(originalBytes);
					string hexadecimal = BitConverter.ToString(encodedBytes).Replace("-", String.Empty);
					Base64 returnValue = Base64.NewBase64FromBase16(hexadecimal);
					this.urlHash = returnValue.ToString();
				}
				return this.urlHash;
			}
		}

		public static int GetChannelsCount() {
			return Count();
		}
	}
}